package maquina.pruebas;
import maquina.ByteString;
import maquina.Alu;
import static org.junit.Assert.assertEquals;
import maquina.excepciones.CadenaNoAptaComoByteException;

import org.junit.Test;

public class AluTest{

	@Test
	public void pruebaOr() throws  CadenaNoAptaComoByteException{
		ByteString entrada1 = new ByteString("00110101");
		ByteString entrada2 = new ByteString("10100111");
		Alu alu = new Alu();
		assertEquals("", alu.or(entrada1,entrada2).aStringBinario(),"10110111" );
		assertEquals("", alu.getZero(),0 );
	}
	
	@Test
	public void pruebaXor() throws  CadenaNoAptaComoByteException{
		ByteString entrada1 = new ByteString("00110101");
		ByteString entrada2 = new ByteString("10100111");
		Alu alu = new Alu();
		assertEquals("", alu.xor(entrada1,entrada2).aStringBinario(),"10010010" );
	}
	
	@Test
	public void pruebaAnd() throws  CadenaNoAptaComoByteException{
		ByteString entrada1 = new ByteString("00110101");
		ByteString entrada2 = new ByteString("10100111");
		Alu alu = new Alu();
		assertEquals("", alu.and(entrada1,entrada2).aStringBinario(),"00100101" );
	}	
	
	@Test
	public void pruebaAnd2() throws  CadenaNoAptaComoByteException{
		ByteString entrada1 = new ByteString("00000000");
		ByteString entrada2 = new ByteString("11111111");
		Alu alu = new Alu();
		assertEquals("", alu.and(entrada1,entrada2).aStringBinario(),"00000000" );
		assertEquals("", alu.getZero(),1 );
	}	
	
	@Test
	public void pruebaRot0() throws  CadenaNoAptaComoByteException{
		ByteString entrada = new ByteString("00110101");
		Alu alu = new Alu();
		assertEquals("", alu.rot(entrada, 0).aStringBinario(),"00110101" );
	}	
	
	@Test
	public void pruebaRot1() throws  CadenaNoAptaComoByteException{
		ByteString entrada = new ByteString("00110101");
		Alu alu = new Alu();
		assertEquals("", alu.rot(entrada,1).aStringBinario(),"10011010" );
	}	
	
	@Test
	public void pruebaRot5() throws  CadenaNoAptaComoByteException{
		ByteString entrada = new ByteString("00110101");
		Alu alu = new Alu();
		assertEquals("", alu.rot(entrada,5).aStringBinario(),"10101001" );
	}
	
	@Test
	public void pruebaRot8() throws  CadenaNoAptaComoByteException{
		ByteString entrada = new ByteString("00110101");
		Alu alu = new Alu();
		assertEquals("", alu.rot(entrada,8).aStringBinario(),"00110101" );
	}
	
	@Test
	public void pruebaSumA2_8mas24() throws  CadenaNoAptaComoByteException{
		ByteString entrada1 = new ByteString("00001000"); // 8
		ByteString entrada2 = new ByteString("00011000"); // 24
		Alu alu = new Alu();
		assertEquals("", alu.suma2(entrada1,entrada2).aStringBinario(),"00100000" ); // 32
		assertEquals("", alu.getCarry(),0 );
		assertEquals("", alu.getOverflow(),0 );
		assertEquals("", alu.getZero(),0 );
		assertEquals("", alu.getNegative(),0 );
	}	
	
	@Test
	public void pruebaSumA2_125mas58() throws  CadenaNoAptaComoByteException{
		ByteString entrada1 = new ByteString("01111101"); // 125
		ByteString entrada2 = new ByteString("00111010"); // 58
		Alu alu = new Alu();
		assertEquals("", alu.suma2(entrada1,entrada2).aStringBinario(),"10110111" ); // 183. Hay overflow.
		assertEquals("", alu.getCarry(),0 );
		assertEquals("", alu.getOverflow(),1 );
		assertEquals("", alu.getZero(),0 );
		assertEquals("", alu.getNegative(),1 );		
	}
	
	@Test
	public void pruebaSumA2_m5masm6() throws  CadenaNoAptaComoByteException{
		ByteString entrada1 = new ByteString("11111011"); // -5
		ByteString entrada2 = new ByteString("11111010"); // -6
		Alu alu = new Alu();
		assertEquals("", alu.suma2(entrada1,entrada2).aStringBinario(),"11110101" ); // -11. No hay overflow. Se descarta el carry.
		assertEquals("", alu.getCarry(),1 );
		assertEquals("", alu.getOverflow(),0 );
		assertEquals("", alu.getZero(),0 );
		assertEquals("", alu.getNegative(),1 );		
	}
	
	@Test
	public void pruebaSumA2_m125masm58() throws  CadenaNoAptaComoByteException{
		ByteString entrada1 = new ByteString("10000011"); // -125
		ByteString entrada2 = new ByteString("11000110"); // -58
		Alu alu = new Alu();
		assertEquals("", alu.suma2(entrada1,entrada2).aStringBinario(),"01001001" ); // -183. Hay overflow. Se descarta el carry.
		assertEquals("", alu.getCarry(),1 );
		assertEquals("", alu.getOverflow(),1 );
		assertEquals("", alu.getZero(),0 );
		assertEquals("", alu.getNegative(),0 );		
	}	

	@Test
	public void pruebaSumA2_57masm29() throws  CadenaNoAptaComoByteException{
		ByteString entrada1 = new ByteString("00111001"); // 57
		ByteString entrada2 = new ByteString("11100011"); // -29
		Alu alu = new Alu();
		assertEquals("", alu.suma2(entrada1,entrada2).aStringBinario(),"00011100" ); // 28. No hay overflow. Se descarta el carry.
		assertEquals("", alu.getCarry(),1 );
		assertEquals("", alu.getOverflow(),0 );
		assertEquals("", alu.getZero(),0 );
		assertEquals("", alu.getNegative(),0 );			
	}
	
	@Test
	public void pruebaSumPF_2c5mas2c5() throws  CadenaNoAptaComoByteException{
		ByteString entrada1 = new ByteString("01010100"); // 2.5
		ByteString entrada2 = new ByteString("01010100"); // 2.5		
		Alu alu = new Alu();
		assertEquals("", alu.sumpf(entrada1, entrada2).aStringBinario(),"01100100" );		
	}
	
	@Test
	public void pruebaSumPF_2c5masm3c5() throws  CadenaNoAptaComoByteException{
		ByteString entrada1 = new ByteString("01010100"); // 2.5
		ByteString entrada2 = new ByteString("11011100"); // -3.5		
		Alu alu = new Alu();
		assertEquals("", alu.sumpf(entrada1, entrada2).aStringBinario(),"11000000" );		
	}	
	
	@Test
	public void pruebaSumPF_8c5mas3c5() throws  CadenaNoAptaComoByteException{
		ByteString entrada1 = new ByteString("01110001"); // 8.5
		ByteString entrada2 = new ByteString("01011100"); // 3.5		
		Alu alu = new Alu();
		assertEquals("", alu.sumpf(entrada1, entrada2).aStringBinario(),"01111000" );		
	}
}